#!/usr/bin/python

import os, hashlib

def write_metadata(directory, metadata):	
	"""
	Writes all collected analysis to the case directory
	"""

	# Write metadata to a file in the case directory
	output_path = os.path.join(directory, "metadata.txt" )
	
	try:
		output = open( output_path , 'w' )
	except:
		print "Error: could not open %s for writing"
		print "metadata.txt will not be created for this analysis run"
	
	print "--- Analysis Complete ---\n"
	print "--- Metadata ---"
	
	for key, value in metadata.iteritems():
		print "%s=%s" % (key, value)
		output.write(str(key) + "=" + str(value) + "\n")
		
	output.close()
	
	print


def initial_metadata(filepath):
	"""
	Generate metadata for the file
	Other defs need access to this data
	"""
	
	#print 'Generating metadata...'

	metadata = {}

	# Get the filename by itself
	metadata['filename'] = os.path.basename(filepath)

	try:
		filehandle = open(filepath, "rb")
		data = filehandle.read()
	except IOError, e:
		print "Error: " + e.strerror
		print "Could not open " + filepath + "for reading"
		sys.exit(1)
		
	metadata['size'] = os.path.getsize(filepath)
	metadata['md5'] = hashlib.md5(data).hexdigest()
	metadata['sha1'] = hashlib.sha1(data).hexdigest()
	metadata['sha256'] = hashlib.sha256(data).hexdigest()
	
	filehandle.close()
	
	return metadata